package com.example.baidumap_a.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bunny.android.library.LoadDataLayout;
import com.example.baidumap_a.Activity.MainActivity;
import com.example.baidumap_a.R;
import com.example.baidumap_a.entity.Charges;
import com.example.baidumap_a.entity.MyAdapter;
import com.example.baidumap_a.entity.UserParkOrder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;


public class ParkOrderFragment extends Fragment {
    private String name;
    private View view;
    private ListView listView;
    private MyAdapter<UserParkOrder> adapter;
    private List<UserParkOrder> mData;
    private List<UserParkOrder> mData2;
    final OkHttpClient client = new OkHttpClient();
    private final static int JSON_OK = 0, JSON_NO = 1;
private LoadDataLayout loadDataLayout;//加载布局
    public ParkOrderFragment(String name) {
        this.name = name;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_park_order, container, false);
        listView = view.findViewById(R.id.listView_park_order);
        loadDataLayout=view.findViewById(R.id.park_ldl);//加载布局
//        loadDataLayout.setBindView(listView);//加载布局绑定listview
        initData();
        return view;
    }

    private void initData() {
        mData = new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    PostHttpRequest();//发送请求
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

        loadDataLayout.showLoading("正在努力加载中", new LoadDataLayout.SetImgCallBack() {
            @Override
            public void setImg(ImageView img) {
                Glide.with(getActivity())
                        .load(R.drawable.gif_loading)
                        .into(img);
            }
        });//显示 load布局 状态正在加载
    }

    private void PostHttpRequest() {
        RequestBody formBody = new FormBody.Builder()
                .add("username",name)
                .build();

        final Request request = new Request.Builder()
                .url("https://d30n971042.imdo.co/park/findUserReserve")
                .post(formBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                myHandler.sendEmptyMessage(JSON_NO);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                if (body != null) {
                    String json = body.string();
                    Gson gson = new Gson();
                    Type type = new TypeToken<List<UserParkOrder>>() {
                    }.getType();
                    mData = gson.fromJson(json, type);
                    body.close();
                    List<UserParkOrder> listCharges = new ArrayList<>();
                    int a = mData.size();
                    for (int i = a - 1; i >= 0; i--) {
                        listCharges.add(mData.get(i));//mData倒序排列

                    }
                    mData2 = listCharges;
                }
                myHandler.sendEmptyMessage(JSON_OK);
            }
        });
    }

    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case JSON_OK:
                    loadDataLayout.showSuccess();//加载布局 状态加载成功
                    if (mData2==null){
                        loadDataLayout.showEmpty();
                    }else if (mData2.size()==0){
                        loadDataLayout.showEmpty();
                    }
                    adapter = new MyAdapter<UserParkOrder>((ArrayList<UserParkOrder>) mData2, R.layout.list_park_order_item) {

                        @Override
                        public void bindView(ViewHolder holder, UserParkOrder obj) {
                            holder.setText(R.id.txt_orderId_rvOrder,obj.getOrderId());
                            holder.setText(R.id.txt_carId_rvOrder,obj.getCarId()+"");
                            holder.setText(R.id.txt_time_rvOrder,obj.getTime_quantum());
                            holder.setText(R.id.txt_date_rvOrder,obj.getOrderTime());

                        }
                    };
                    listView.setAdapter(adapter);
                    break;
                case JSON_NO:
                    loadDataLayout.showError("网络连接错误");//加载布局 状态加载失败

                    break;
            }
        }
    };
}
